<?php
namespace preg;

use think\Curler; 
use app\admin\controller\WeiBo;

class PregMatch
{
	//public static $time = time();
	//匹配关注数量
	protected static $follow = '/<strong class=\\\"W_f16\\\">([0-9]*)<\\\\\/strong><span class=\\\"S_txt2\\\">关注<\\\\\/span>/';
	//匹配粉丝数
	protected static $followed = '/<strong class=\\\"W_f16\\\\">([0-9]*)<\\\\\/strong><span class=\\\"S_txt2\\\">粉丝<\\\\\/span>/';
	//匹配微博数量
	protected static $wCount = '/<strong class=\\\"W_f16\\\">([0-9]*)<\\\\\/strong><span class=\\\"S_txt2\\\">微博<\\\\\/span>/';


	public static function searchUser($searchName)
	{	
		$url = 'http://s.weibo.com/user/' . urlencode($searchName) . '&Refer=SUer_box';
        $outPut = Curler::request('GET', $url);
        file_put_contents(WeiBo::$CONFIG['log_file'],  date('Y-m-d H:i:s') . ':正在搜索用户' . $searchName . PHP_EOL, FILE_APPEND);
        //$outPut = file_get_contents('./static/info/' . $searchName . '.txt');
        $link = self::getSearchLink($outPut, $searchName);
        if (!empty($outPut))
        {
        	file_put_contents( './static/info/' . $searchName . '.txt', $outPut);
        }
        file_put_contents(WeiBo::$CONFIG['log_file'],  date('Y-m-d H:i:s') . ':保存用户' . $searchName . '信息成功' . PHP_EOL, FILE_APPEND);
        if (!empty($link))
        {
        	self::getAllInfo($link, $searchName);
        	return true;
        }
        else
        {
        	file_put_contents(WeiBo::$CONFIG['log_file'],  date('Y-m-d H:i:s') . ':用户' . $searchName . '不存在' . PHP_EOL, FILE_APPEND);
        	return false;
        }
	}
	public static function getUserInfo($searchName)
	{

		$user = [];
		$name = './static/info/' . $searchName . 'home.txt';
		if (is_file($name))
		{
			$pre = array(" ","　","\t","\n","\r","\\r","\\t","\\n");
		$rep = array('','','','','','');
		$data = str_replace($pre, $rep, file_get_contents($name));

		$uname = '/<title>.*<\/title>/';
		preg_match($uname, $data, $uinfo);
		$uinfo = implode('', $uinfo);
		//var_dump( str_replace('的微博_微博', '', strip_tags($uinfo)));
		$uinfo = str_replace('的微博_微博', '', strip_tags($uinfo));
		$user['n_name'] = $uinfo;


		$guan = '/<strong class=\\\".{0,10}\\\">[0-9]*<\\\\\/strong><span class=\\\".{0,10}\\\">关注<\\\\\/span>/x';
		preg_match($guan, $data, $uinfo);
		$uinfo = implode('', $uinfo);
		//var_dump(str_replace('关注', '', strip_tags($uinfo)));
		$uinfo = str_replace('关注', '', strip_tags($uinfo));
		$user['attention'] = empty($uinfo) ? 0 : $uinfo;


		$fen = '/<strong class=\\\".{0,10}\\\">[0-9]*<\\\\\/strong><span class=\\\".{0,10}\\\">粉丝<\\\\\/span>/x';
		preg_match($fen, $data, $uinfo);
		$uinfo = implode('', $uinfo);
		//var_dump(str_replace('粉丝', '', strip_tags($uinfo)));
		$uinfo = str_replace('粉丝', '', strip_tags($uinfo));
		$user['fans'] = empty($uinfo) ? 0 : $uinfo;


		$wei = '/<strong class=\\\".{0,10}\\\">[0-9]*<\\\\\/strong><span class=\\\".{0,10}\\\">微博<\\\\\/span>/x';
		preg_match($wei, $data, $uinfo);
		$uinfo = implode('', $uinfo);
		//var_dump(str_replace('微博', '', strip_tags($uinfo)));
		$uinfo = str_replace('微博', '', strip_tags($uinfo));
		$user['weibo_count'] = empty($uinfo) ? 0 : $uinfo;

		$where = '/<spanclass=\\\"item_textW_fl\\\">.{0,80}<\\\\\/span>/x';
		preg_match($where, $data, $uinfo);
		$uinfo = implode('', $uinfo);
		//var_dump(strip_tags($uinfo));
		$uinfo = strip_tags($uinfo);
		$user['come_from'] = $uinfo;

		$unver = '/<spanclass=\\\"S_txt2\\\">.{0,20}<\\\\\/span><atarget=\\\"_blank\\\"href=\\\".{0,200}\\\">.{0,50}<\\\\\/a>/x';
		preg_match($unver, $data, $uinfo);
		if (empty($uinfo))
		{
			$unver = '/<spanclass=\\\"S_txt2\\\">.{0,20}<\\\\\/span>.{0,30}<\\\\\/span>/';
			preg_match($unver, $data, $uinfo);
		}
		$uinfo = implode('', $uinfo);
		//var_dump(strip_tags($uinfo));
		$uinfo = strip_tags($uinfo);
		$user['university'] = $uinfo;

		$birthday = '/<spanclass=\\\"item_textW_fl\\\">.{0,80}<\\\\\/span>/';
		preg_match_all($birthday, $data, $uinfo);
		//var_dump(strip_tags($uinfo[0][2]));
		//$uinfo = strip_tags($uinfo[0][2]);
		//$user['birthday'] = $uinfo;
		return $user; 
		}
		else
		{
			return array('issearch' => 0, );
		}
		
	}

	/**
	*匹配搜索到的用户链接
	*
	*/
	public static function getSearchLink($data, $searchName)
	{
		$link = [];
		//匹配链接
		$href = '/<a class=\\\"W_linkb\\\" target=\\\"_blank\\\" href=\\\"http:\\\\\/\\\\\/weibo.com(?:\\\\\/u)?\\\\\/(?:[0-9a-zA-Z]{0,40})\?refer_flag=1001030201_\\\\\" class=\\\"wb_url\\\" suda-data=\\\"key=tblog_search_user&value=user_feed_1_url\\\">http:\\\\\/\\\\\/weibo.com(?:\\\\\/u)?\\\\\/(?:[0-9a-zA-Z]{0,40})<\\\\\/a>/';
		//$href = '/<a class=\\\"W_linkb\\\" target=\\\"_blank\\\" href=\\\"http:\\\\\/\\\\\/weibo.com\\\\\/[a-z]+\?refer_flag=[0-9]*_\\\"class=\\\"wb_url\\\" suda-data=\\\"key=tblog_search_user&value=user_feed_1_url\\\">http:\\\\\/\\\\\/weibo.com\\\\\/[a-z]+<\\\\\/a>/';
		preg_match($href, $data, $info);
		//var_dump($info);
		//echo '<br>';
		$info = implode('', $info);
		$urlArr = str_replace('\\', '', strip_tags($info)); 
		return $urlArr;
	}
	public static function getAllInfo($url, $searchName)
	{
        $outPut = Curler::request('GET', $url);
        file_put_contents(WeiBo::$CONFIG['log_file'], date('Y-m-d H:i:s') . ':进入用户' . $searchName . '主页' . PHP_EOL, FILE_APPEND);
        //$outPut = file_get_contents('./static/info/' . $searchName . '.txt');
        if (!empty($outPut))
        {
        	file_put_contents( './static/info/' . $searchName . 'home.txt', $outPut);	
        }
        file_put_contents(WeiBo::$CONFIG['log_file'], date('Y-m-d H:i:s') . ':保存用户' . $searchName . '主页信息成功' . PHP_EOL, FILE_APPEND);
	}
}